<?php

namespace app\api\controller\haipi;

use app\common\controller\Frontend;
use app\common\model\haipi\Discuz as DiscuzModel;
use app\common\model\haipi\HomeRecommendation;
use think\facade\Db;

class Discuz extends  Frontend
{
    protected array $noNeedLogin = ["*"];
    protected array $noNeedRight = ["*"];

    /**
     * 列表
     */
    public function index(){
        $page = $this->request->get("page",1);
        $pagesize = $this->request->get("pagesize",20);
        $keyword = $this->request->get("keyword","");
        $cityCode = $this->request->get("city_code");
        $districtCode = $this->request->get("district_code");
        if(empty($cityCode)){
            $this->error("缺少参数");
        }
        $showopage = $page == 1 ? true : false;
        $list = DiscuzModel::where('status', DiscuzModel::STATUS_NORMAL)
        ->where(function ($query)use($keyword)  {
            if(!empty($keyword)){
                $query->where('title','like',"%{$keyword}%");
                $query->whereOr('address','like',"%{$keyword}%");
            }
        })
        ->where('status', DiscuzModel::STATUS_NORMAL)
        ->where(function ($query) use($cityCode, $districtCode){      
                if(!empty($cityCode)){
                    $query->where('city_code', $cityCode);
                }
                if(!empty($districtCode)){
                    $query->where('district_code', $districtCode);  
                }
            })   
            ->field(['id', 'thumb_image', 'title'])    
            ->order('weigh', 'desc')
            ->paginate([
                'list_rows'=> $pagesize,
                'page' => $page,
            ], !$showopage);
            $data = [
                'items' => $list->items()
            ];
            if($showopage){
                $data['pageInfo']= [
                    'total' => $list->total(),
                    'totalPage' => $list->lastPage(),
                    'currentPage' => $page,
                ];
            }
        $this->success('', $data);
    }

    public function home(){
        $cityCode = $this->request->get('city_code');
        $where = [];
        $ids = HomeRecommendation::where('type', HomeRecommendation::TYPE_DISCUZ)->column('source_id');
        $list = DiscuzModel::field(['id', 'thumb_image', 'title'])
        ->order('weigh','desc')
        ->where($where)
        ->where('audit_status', DiscuzModel::AUDIT_STATUS_PASS)
        ->where('status', DiscuzModel::STATUS_NORMAL)
        ->where(function($query) use($ids,$cityCode){
            if($cityCode){
                $query->where('city_code', $cityCode);
            }
            if(!empty($ids)){
                $query->whereIn('id', $ids);
            }
        })
        ->whereNotNull('contact')
        ->limit(6)
        ->select();
        $this->success('', $list);
    }

     public function read(){
        $id = $this->request->get('id');
        if(!$id){
            $this->error("缺少参数");
        }
        $data = DiscuzModel::field(['id', 'thumb_image', 'title', 'images', 'age', 'height','weight','bust','address', 'price', 'service', 'publish_time','service'])->where("id", $id)->find();
        $this->success('',$data);
     }
}